﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Threading;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Reflection;

namespace LevenBlog.Core.Data
{
    public abstract class BaseEntityHelper
    {
        public TableAttribute GetTableInfo(IEntity entity)
        {
            return GetInfo(entity).Table;
        }

        public ColumnAttribute[] GetColumnInfo(IEntity entity)
        {
            return GetInfo(entity).Columns;
        }

        private TableInfo GetInfo(IEntity entity)
        {
            return GetInfo(entity.GetType());
        }

        private TableInfo GetInfo<EntityType>()
        {
            Type type = typeof(EntityType);
            return GetInfo(type);
        }

        public LinkTableAttribute[] GetLinkTable(IEntity entity)
        {
            return GetInfo(entity).LinkTable;
        }

        public LinkTablesAttribute[] GetLinkTables(IEntity entity)
        {
            return GetInfo(entity).LinkTables;
        }

        public TableInfo GetInfo(Type type)
        {
            TableInfo info = EntityTypeCache.Get(type);
            if (info == null)
            {
                TableAttribute[] tas = type.GetCustomAttributes(typeof(TableAttribute), false) as TableAttribute[];
                List<ColumnAttribute> caList = new List<ColumnAttribute>();
                List<LinkTableAttribute> linkTableList = new List<LinkTableAttribute>();
                List<LinkTablesAttribute> linkTablesList = new List<LinkTablesAttribute>();
                PropertyInfo[] pis = type.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    ColumnAttribute[] cas = pi.GetCustomAttributes(typeof(ColumnAttribute), false) as ColumnAttribute[];
                    LinkTableAttribute[] lt = pi.GetCustomAttributes(typeof(LinkTableAttribute), false) as LinkTableAttribute[];
                    LinkTablesAttribute[] lts = pi.GetCustomAttributes(typeof(LinkTablesAttribute), false) as LinkTablesAttribute[];
                    if (cas.Length > 0)
                    {
                        caList.Add(cas[0]);
                    }
                    if (lt.Length > 0)
                    {
                        linkTableList.Add(lt[0]);
                    }
                    if (lts.Length > 0)
                    {
                        linkTablesList.Add(lts[0]);
                    }
                }
                info = new TableInfo();
                info.Table = tas[0];
                info.Columns = caList.ToArray();
                info.Properties = type.GetFastProperties();
                info.LinkTable = linkTableList.ToArray();
                info.LinkTables = linkTablesList.ToArray();
                EntityTypeCache.Insert(type, info);
            }
            return info;
        }
    }
}
